package fs.component {
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author F.S.
	 */
	public class ButtonGroup {
		private static const DIC_GROUP:Dictionary = new Dictionary(true);
		public function ButtonGroup(noInstance:Object) {}
		internal static function add(radioButton:RadioButton):void {
			var groupName:String = radioButton.groupName;
			
			if (groupName == "" || groupName == null) {
				return;
			}
			
			if (!DIC_GROUP.hasOwnProperty(groupName)) {
				DIC_GROUP[groupName] = new Dictionary(true);
			}
			
			var dicBtns:Dictionary = DIC_GROUP[groupName];
			var rand:String;
			do {
				rand = "b_" + Math.random();
			}while (dicBtns.hasOwnProperty(rand))
			
			dicBtns[rand] = radioButton;
			radioButton.buttonGroupIndex = rand;
			//radioButton.buttonGroup = dicBtns;
		}
		internal static function remove(radioButton:RadioButton):void {
			var groupName:String = radioButton.groupName;
			if (groupName == "" || groupName == null) {
				return;
			}
			
			var dicBtns:Dictionary = DIC_GROUP[groupName];
			if (dicBtns.hasOwnProperty(dicBtns.buttonGroupIndex)) {
				delete dicBtns.buttonGroupIndex;
			}
		}
		
		static public function setOthersOffInGroup(radioButton:RadioButton):void {
			var groupName:String = radioButton.groupName;
			if (groupName == "" || groupName == null) {
				return;
			}
			
			var dicBtns:Dictionary = DIC_GROUP[groupName];
			for each(var rb:RadioButton in dicBtns) {
				rb.setOn(radioButton == rb); 
			}
		}
		
		static public function getSelectedInGroup(groupName:String):RadioButton {
			if (groupName == "" || groupName == null) {
				return null;
			}
			
			var dicBtns:Dictionary = DIC_GROUP[groupName];
			for each(var rb:RadioButton in dicBtns) {
				if (rb.isOn) {
					return rb;
				}
			}
			
			return null;
		}
	}

}